﻿using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace ES.Client.Filter
{
    /// <summary>
    /// 顶层嵌套字段过滤条件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RootNestedFieldBuilder<T> : BaseFilter<T>, IFilterBuilder where T : class
    {
        internal RootNestedFieldBuilder(string index, string docType)
        {
            this.Index = index;
            this.DocType = docType;
        }

        public IEnumerable<QueryContainer> EQ<TValue>(string field, TValue value)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> NE<TValue>(string field, TValue value)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> GT<TValue>(string field, TValue value)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> GTE<TValue>(string field, TValue value)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> LT<TValue>(string field, TValue value)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> LTE<TValue>(string field, TValue value)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        /// <summary>
        /// 判断文档是否包含某个字段，字段值既不等于null也不等于空字符串
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public IEnumerable<QueryContainer> Exists(string field)
        {
            return this.IsNotNullAndEmpty(field);
        }

        /// <summary>
        /// 通过给定字段获取文档，字段值既不等于null也不等于空字符串
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public IEnumerable<QueryContainer> IsNotNullAndEmpty(string field)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            var script = string.Format("_source['{0}'] != null && _source['{0}'].size() > 0", field);
            containers.Add(Query<T>.Script(f => f.Inline(script)));
            return containers;
        }

        /// <summary>
        /// 判断文档是否不包含某个文档，字段值等于null或者等于空字符串
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public IEnumerable<QueryContainer> Missing(string field)
        {
            return this.IsNullOrEmpty(field);
        }

        /// <summary>
        /// 通过给定字段获取文档，字段值等于null或者等于空字符串
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public IEnumerable<QueryContainer> IsNullOrEmpty(string field)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            var script = string.Format("_source['{0}'] == null || _source['{0}'].size() == 0", field);
            containers.Add(Query<T>.Script(f => f.Inline(script)));
            return containers;
        }

        public IEnumerable<QueryContainer> Prefix(string field, string query)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> Like(string field, string keyword)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> Regex(string field, string regex)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }

        public IEnumerable<QueryContainer> IN<TValue>(string field, IEnumerable<TValue> values)
        {
            List<QueryContainer> containers = new List<QueryContainer>();
            return containers;
        }
    }
}